﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Drawing;
using System.Xml;
using Google.GData.Photos;
using Google.GData.Extensions.MediaRss;
using Google.GData;

namespace Astro.Core
{
    public class GooglePicasa
    {
        private String googleAuthToken = null;
        private String user = null;
        private String userOnFirst = null;
        private PicasaService picasaService = new PicasaService("AstroCMS");
        private PicasaFeed picasaFeed = null;

        public GooglePicasa()
        {
            Instance(SiteSettings.Get().GoogleUser, SiteSettings.Get().GooglePassword);
        }

        public GooglePicasa(string userName, string password)
        {
            Instance(userName, password);
        }

        void Instance(string userName, string password)
        {
            if (this.googleAuthToken == null)
            {
                GoogleClientLogin loginDialog = new GoogleClientLogin(picasaService, userName, password);

                loginDialog.Login();
                this.googleAuthToken = loginDialog.AuthenticationToken;
                this.user = loginDialog.User;
                this.userOnFirst = this.user.Split('@')[0];

                if (this.googleAuthToken != null)
                {
                    picasaService.SetAuthenticationToken(loginDialog.AuthenticationToken);
                    AlbumQuery query = new AlbumQuery();
                    query.Uri = new Uri(PicasaQuery.CreatePicasaUri(this.user));
                    this.picasaFeed = this.picasaService.Query(query);
                }
            }
        }

        public void DeleteAlbum()
        {
            AlbumQuery query = new AlbumQuery();
            query.Uri = new Uri(PicasaQuery.CreatePicasaUri(this.user));
            this.picasaFeed = this.picasaService.Query(query);
            if (this.picasaFeed != null && this.picasaFeed.Entries.Count > 0)
            {
                foreach (PicasaEntry entry in this.picasaFeed.Entries)
                {
                    entry.Delete();
                }
            }
        }

        public PicasaEntry AddAlbum(string name, string description)
        {
            AlbumEntry entry = new AlbumEntry();
            AlbumAccessor acc = new AlbumAccessor(entry);
            entry.Title.Text = name;
            entry.Summary.Text = description;
            acc.Access = "public";
            acc.CommentingEnabled = false;
            PicasaEntry pe = this.picasaService.Insert(this.picasaFeed, entry);
            return pe;

        }

        public string AddPhoto(string albumId, Stream data)
        {
            string result = null;
            PicasaEntry entry = new PhotoEntry();
            entry.MediaSource = new Google.GData.Client.MediaFileSource(data, "telligentcms.jpg", "image/jpeg");
            Uri albumUri = new Uri(String.Format("http://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}", this.userOnFirst, albumId));
            PicasaEntry newEntry = this.picasaService.Insert(albumUri, entry);
            result = newEntry.Content.Src.Content;
            return result;
        }

        public string AddPhoto(string albumId, string path)
        {
            string result = null;
            //WebClient webClient = new WebClient();
            //string tempFile = HttpContext.Current.Server.MapPath(String.Format("~/__utility/pictemp/{0}.jpg", Guid.NewGuid().ToString()));
            //webClient.DownloadFile(src, tempFile);
            using (StreamReader reader = new StreamReader(path))
            {
                PicasaEntry entry = new PhotoEntry();
                entry.MediaSource = new Google.GData.Client.MediaFileSource(reader.BaseStream, "telligentcms.jpg", "image/jpeg");
                Uri albumUri = new Uri(String.Format("http://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}", this.userOnFirst, albumId));
                PicasaEntry newEntry = this.picasaService.Insert(albumUri, entry);
                result = newEntry.Content.Src.Content;
                File.Delete(path);
            }
            return result;
        }
    }

    class UserState
    {
        public enum OperationType
        {
            upload,
            download,
            downloadList,
            query,
            queryForBackup
        }

        public string filename;
        public OperationType opType;
        public PicasaFeed feed;
        public int counter = 0;
        public string foldername;

    }
}